package com.lhh.springcloud.controller;

import com.lhh.entity.Member;
import com.lhh.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 李洪豪
 * @Sno 20220301044
 */
@RestController
public class MemberConsumerController {
    private static final Logger log = LoggerFactory.getLogger(MemberConsumerController.class);
    //这行代码的作用是为当前类 MemberConsumerController 创建一个日志记录器（Logger）实例，用于在程序中输出日志信息。
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private DiscoveryClient discoveryClient;
    //可通过DiscoveryClient相关方法得到eureka-client注册到eureka-service的信息
    /**
     * 1.VIPMEMBER-SERVICE-PROVIDER就是服务提供方[集群10001,10002]注册到Eureka Servcer的名称，可以在其yaml文件中查看，也可以在Eureka-Service页面中查看
     * 2.VIPMEMBER-SERVICE-PROVIDER目前有两个Availability Zones：分别是192.168.204.1:vipmember-service-provider:10002 , 192.168.204.1:vipmember-service-provider:10001(通过服务管理页面查看)
     * 3.需要为RestTemplate配置增加一个注解@LoadBanlanced，赋予RestTemplate负载均衡的能力，默然是轮询算法，也可自己配置
     */

    private static final String MEMBER_SERVICE_PROVIDER_URL = "http://VIPMEMBER-SERVICE-PROVIDER";
    @PostMapping("/member/consumer/save")
    //无@RequestBody注解，请求时需要以表单的方式，否则封装不成功
    public Result<Member> saveMember(Member member){
        //RestTemplate模版请求是以json的格式请求,因此接收方要想完成数据的封装，需要使用@RequestBody注解
        //restTemplate接收到的类型(Result)是由接收方返回的数据类型决定的
        //是postForObject，还是getForObject，还是putForObject，还是deleteForObject，是由接收方的请求类型决定的
        //member是指将member对象发送给接收方，完成接收方方法参数的封装
        Result result = restTemplate.postForObject(MEMBER_SERVICE_PROVIDER_URL+"/member/save",member,Result.class);
        return result;
    }

    @PostMapping("/member/consumer/query/{id}")
    public Result<Member> queryMemberById(@PathVariable Long id){
        Result result = restTemplate.getForObject(MEMBER_SERVICE_PROVIDER_URL+"/member/query/"+id,Result.class);
        return result;
    }

    @GetMapping("/member/consumer/discovery")
    public Object discoveryClients(){
        List<String> services = discoveryClient.getServices();//得到eureka-service管理页面的Application，即得到每一个微服务的集群
        for (String service : services){
            log.info("服务名：{}",service);
            List<ServiceInstance> instances = discoveryClient.getInstances(service);//将得到的集群进行遍历
            for (ServiceInstance instance : instances){
                log.info("id={},Host={},port={},uri={}",
                        instance.getServiceId(),instance.getHost(),instance.getPort(),instance.getUri());
            }
        }
        return services;
    }
}
